﻿using MediatR;
using ProduceBasicData.Api.Application.Command.DeviceManage;
using ProduceBasicData.Api.Application.Command.Material.Craft;
using ProduceBasicData.Domain.DeviceManage;
using ProduceBasicData.Domain.Inventories;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Material.Craft
{
    /// <summary>
    /// 批量删除工艺路线命令处理器
    /// </summary>
  
    public class DeleteCraftCommandHandler : IRequestHandler<DeleteCraftCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<CraftModel> _craft;

        public DeleteCraftCommandHandler(IBaseRespository<CraftModel> craft)
        {
            _craft = craft;
        }

        /// <summary>
        /// 处理删除工艺路线命令
        /// </summary>
        /// <param name="request">删除工艺路线命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>包含操作结果的ApiResult</returns>
        public async Task<ApiResult<int>> Handle(DeleteCraftCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证输入参数
                if (string.IsNullOrEmpty(request.Ids))
                {
                    return new ApiResult<int>
                    {
                        Msg = "请选择要删除的工艺路线",
                        Code = ApiEnum.Failed
                    };
                }

                // 将字符串Ids转换为整数数组
                var idArray = request.Ids.Split(',', StringSplitOptions.RemoveEmptyEntries)
                    .Select(id => int.TryParse(id.Trim(), out var result) ? result : -1)
                    .Where(id => id > 0)
                    .ToArray();

                if (idArray.Length == 0)
                {
                    return new ApiResult<int>
                    {
                        Msg = "无效的工艺路线ID格式",
                        Code = ApiEnum.Failed
                    };
                }

                // 根据ID获取工艺路线数据
                var craftList = await _craft.GetRangeAsync(idArray);
                if (craftList.Count == 0)
                {
                    return new ApiResult<int>
                    {
                        Msg = "未找到指定的工艺路线",
                        Code = ApiEnum.Failed
                    };
                }

                // 执行批量软删除
                foreach (var item in craftList)
                {
                    item.Isdel = true;
                }
                await _craft.UpdateRange(craftList);

                // 返回成功结果
                return new ApiResult<int>
                {
                    Code = ApiEnum.Success,
                    Msg = "批量删除工艺路线成功",
                    Data = craftList.Count
                };
            }
            catch (Exception ex)
            {
                // 异常时返回错误信息
                return new ApiResult<int>
                {
                    Code = ApiEnum.Failed,
                    Msg = $"删除工艺路线时发生错误: {ex.Message}"
                };
            }
        }
    }
}
